let express = require('express');
let router = express.Router();

const fileService = require('../service/fileService');

// 引入jwt验证方法的中间件
const checkJWTMiddleWare = require('../middleWare/checkJWTMiddleWare');
router.use(checkJWTMiddleWare);

// 创建图片上传节点
router.get('/createFileNode', async function(req, res, next) {
    let result = await fileService.createFileNode();
    res.status(result.success ? 200 : 400).json(result);
});

// 获取上传预签名URL
router.post('/getUploadUrl', async function(req, res, next) {
    let imgId = req.body.imgId;
    let result = await fileService.getUploadUrl(imgId);
    res.status(result.success ? 200 : 400).json(result);
});

// 标记图片上传完成
router.post('/markUploadComplete', async function(req, res, next) {
    let imgId = req.body.imgId;
    let result = await fileService.markUploadComplete(imgId);
    res.status(result.success ? 200 : 400).json(result);
});

// 加载图片
router.post('/downloadPublicFile', async function(req, res, next) {
    let uid = req.jwtResult.data.uid;
    let imgId = req.body.imgId;
    if (!imgId) {
        res.status(400).json({ success: false, message: 'imgId 不能为空' });
    }
    let result = await fileService.downloadPublicFile(imgId, uid);
    res.status(result.success ? 200 : 400).json(result);
});

module.exports = router;